package com.mytx.live.config;

import com.mytx.common.utils.DateUtils;
import io.agora.signal.Signal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.NoSuchAlgorithmException;
import java.util.Date;

//@Configuration
public class AgoraConfig {
    private static Logger logger = LoggerFactory.getLogger(AgoraConfig.class);

    @Value("${agora_app_id}")
    private String agoraAppId;
    @Value("${agora_certificate}")
    private String agoraCertificate;
    @Value("${system_service_id}")
    private String systemServiceId;

    @Bean
    public Signal.LoginSession createSignal() throws NoSuchAlgorithmException {
//"1:appId:expiredTime:md5(account + appId + appCertificate + expiredTime)"

        String agoraToken = SignalingToken.getToken(agoraAppId, agoraCertificate, systemServiceId, (int) DateUtils.addDay(new Date(), 365).getTime());
        Signal signal = new Signal(agoraAppId);
       //signal.setDoLog(true);
        Signal.LoginSession login = signal.login(systemServiceId, "_no_need_token", new MycallBack());

        return login;

    }


    private class MycallBack extends Signal.LoginCallback {

        @Override
        public void onLoginSuccess(Signal.LoginSession session, int uid) {
            logger.info("onLoginSuccess:声网登录成功,uid:{}", uid);
        }


        @Override
        public void onLoginFailed(Signal.LoginSession session, int ecode) {
            logger.error("onLoginFailed:ecode:{}", ecode);
        }

        @Override
        public void onLogout(Signal.LoginSession session, int ecode) {
            logger.info("onLogout:ecode:{}", ecode);

        }
    }

}
